<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of pessoa_cadastro_model
 *
 * @author link
 */
class Tags_info_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->config('tags/parametros');
    }

    public function consultarTag($_tag) {
        $this->db->where('nome', $_tag);

        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblTagsInfo'));

        //pre($this->db->last_query());

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return null;
        }
    }

    public function cadastrar($_dados = array()) {
        $tag = $this->consultarTag($_dados['nome']);

        if ($tag != null) {
            return $tag->tags_id;
        } else {
            $tag = array();
            $tag['id'] = null;

            $tag = $this->cadastrarTag($tag);
            if ($tag != null) {
                $_dados['tags_id'] = $tag;
                $_dados['local_idiomas_id'] = 2;
                $this->db->set($_dados);
                //pre('['.$this->config->item('dbCliente').']' . '[' . $this->config->item('tblAnuncioVeiculos') . ']');
                $this->db->insert($this->config->item('dbCliente') . $this->config->item('tblTagsInfo'));
                if ($this->db->affected_rows() > 0) {
                    return $tag;
                }
            }
            return null;
        }
    }

    public function cadastrarTag($_dados = array()) {
        $this->db->set($_dados);
        $this->db->insert($this->config->item('dbCliente') . $this->config->item('tblTags'));
        if ($this->db->affected_rows() > 0) {
            return $this->db->insert_id();
        } else {
            return NULL;
        }
    }
}

?>
